package com.example.productserver.controller;

import com.alibaba.fastjson.JSON;
import com.example.productserver.config.Cloud7DayProperties;
import com.example.productserverapi.entity.Product;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * @author benjamin_5
 * @Description
 * @date 2023/2/14
 */
@RestController
@RequestMapping("product")
public class ProductController {

    @Autowired
    private Cloud7DayProperties properties;

    @Value("${common.name}")
    private String name;

    @Value("${server.port}")
    private String port;

    @GetMapping("get")
    public String getProduct(){
        return "product";
    }

    @GetMapping("getInfo")
    public String getInfo() throws InterruptedException {
        Thread.sleep(1000);
        return properties.toString();
    }

    @GetMapping("common")
    public String getCommon(){
        return "common: " + name;
    }

    @GetMapping("getByName")
    public String getByName( String name){
        return "product " + name;
    }

    @PostMapping("getByBody")
    public String getByBody(@RequestBody Product product){
        return "product:" + JSON.toJSONString(product);
    }

    @GetMapping("getByBody2")
    public String getByBody2(Product product){
        return "product:" + JSON.toJSONString(product);
    }

    @GetMapping(value = "getByBody3", consumes = MediaType.APPLICATION_JSON_VALUE)
    public String getByBody3(@RequestBody Product product){
        return "product:" + port + "|" + JSON.toJSONString(product);
    }
}
